package com.tobeface.sns.infrastructure.persist.pal.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.kissme.core.orm.Page;
import com.tobeface.sns.domain.User;
import com.tobeface.sns.infrastructure.persist.CrudMongoRepositorySupport;
import com.tobeface.sns.infrastructure.persist.pal.FriendRepository;

/**
 * @author Dylan
 * @time 2012-7-28 
 * @by this class ,we can find friend,but,we don't make friend
 */
@Repository
public class FriendRepositoryImpl extends CrudMongoRepositorySupport<User, String> implements FriendRepository {

	@Autowired
	protected FriendRepositoryImpl(MongoOperations mongoOperations) {
		super(mongoOperations);
	}

	public Page<User> findByAddress(Page<User> page,User self) {
		page.getParams().put("address", self.getProfiles().getAddress());
		page = findPage(page);
		return page;
	}

	public Page<User> findByVocation(Page<User> page,User self) {
		page.getParams().put("", "");
		page = findPage(page);
		return page;
	}

	public Page<User> findByName(Page<User> page,String name) {
		List<User> result = getMongoOperations().find(new Query(Criteria.where("profiles.name").is(name)), User.class);
		page.setResult(result);
		return page;
	}

	/* (non-Javadoc)
	 * @see com.sinzv.sns.infrastructure.persist.pal.FriendRepository#makeFriend(com.sinzv.sns.domain.User, com.sinzv.sns.domain.User)
	 */
	@Override
	public void makeFriend(User self, User friend) {
		save(self.makeFriend(friend));
	}

	/* (non-Javadoc)
	 * @see com.tobeface.sns.infrastructure.persist.pal.FriendRepository#breakFriend(com.tobeface.sns.domain.User, com.tobeface.sns.domain.User)
	 */
	@Override
	public void breakFriend(User self, User friend) {
		save(self.breakFriend(friend));
	}

}
